<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/displaytag-11.tld" prefix="display"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.*"%>
<%@ page
	import="co.gov.medellin.ssti.sub.model.vo.AtributoVO,co.gov.medellin.ssti.sub.model.vo.ModificarBOTVO"%>
<%@ page import="co.gov.medellin.ssti.sub.model.vo.ValorFlexibleVO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" isELIgnored="false"%>
	
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript"
	src="<%= request.getContextPath() + "/js/popUP.js" %>"></script>
<link href="<%= request.getContextPath() + "/css/bloques.css" %>"
	rel="stylesheet" type="text/css">
<link href="<%= request.getContextPath() + "/css/table.css" %>"
	rel="stylesheet" type="text/css">
<script src="<%=request.getContextPath()%>/js/dateValidation.js"
	type="text/javascript"></script>
<script type="text/javascript"
	src="<%=request.getContextPath() + "/js/jquery-1.8.0.js"%>"></script>
<script type="text/javascript"
	src="<%=request.getContextPath()
					+ "/js/jquery-ui-1.10.3.custom.min.js"%>"></script>
<script type="text/javascript"
	src="<%=request.getContextPath()
					+ "/js/jquery.ui.datepicker-es.js"%>"></script>
<script src="<%=request.getContextPath()%>/js/dateValidation.js"
	type="text/javascript"></script>					
<link rel="stylesheet"
	href="<%=request.getContextPath()
					+ "/css/ui-lightness/jquery-ui-1.10.3.custom.css"%>" />

<script type="text/javascript">
$(document).ready(function() {
	
	$("#guardarBtn").click(function(){	
		$("#mainForm").attr('target','_self');		
		if(!isDate($("#date").val())){
			alert('Fecha de otorgacion no valida.');
			return false;
		}
		var submit = true;
		// recorre elementos de clase calendario para validar fechas.
		$('.calendario').each(function(){
			var valorFecha =  $.trim($(this).val());
			if(valorFecha.length != 0){
			    if(!isDate(valorFecha)){
			    	alert('Fecha incorrecta: ' + $(this).val());
			    	$(this).focus();	
			    	submit = false;
					return false;
			    }
			}
			
		 });	
		if(submit){			
			$("#mainForm").submit();
		}
		
	});
	
});
</script>

<!-- DatePickers -->
<script type="text/javascript">
$.datepicker.setDefaults({
	  showOn: "button",
	  buttonImageOnly: true,
	  buttonImage: "<%=request.getContextPath() + "/images/Calendar.png"%>",
	  buttonText : "Calendario"
	});
	$(function() {
		$("#date").datepicker({
			changeMonth : true,
			changeYear : true
		});
	});
	
	$(function() {
		$(".calendario").datepicker({
			changeMonth : true,
			changeYear : true
		});
	});
</script>
<title>Modificacion Beneficio Otorgado</title>

<script type="text/javascript">    

    var a, mes, dia, anyo, febrero;
 
function validate(field) {
    var valid = "0123456789,";
    var ok = "yes";
    var temp;
    for (var i=0; i<field.value.length; i++) {
    temp = "" + field.value.substring(i, i+1);
    if (valid.indexOf(temp) == "-1") ok = "no";
    }
    if (ok == "no") {
    alert("El Campo es Numerico");
    field.focus();
    field.select();
       }
    }
    
function validateletras(field) {
    var valid = "ABCDEFGHIJKLMNÃOPQRSTUVWXYZabcdefghijklmnÃ±opqrstuvwxyz";
    var ok = "yes";
    var temp;
    for (var i=0; i<field.value.length; i++) {
    temp = "" + field.value.substring(i, i+1);
    if (valid.indexOf(temp) == "-1") ok = "no";
    }
    if (ok == "no") {
    alert("El Campo es solo letras (A-Z)");
    field.focus();
    field.select();
       }
    }
</script>


</head>
<body>
	<form name="frmIngresoDatos" id="mainForm"
		action="<%= request.getContextPath() + "/beneficios/GuardarModificacionBOT.do"%>"
		method="get">
		<c:if test="${empty sessionScope.ListaSede}">		
			<div align="center">Verifique la vigencia del Beneficio.</div>		
		</c:if>

		<c:if test="${not empty sessionScope.ListaSede}">	
			<c:if test="${empty sessionScope.beneSelected}">				
				<tr>
					<td class="titulosenblanco" colspan="3">
						No se encontr&oacute; el beneficiario al que se le otorgar&aacute; un beneficio.
					</td>
				</tr>			
			</c:if>
			<%-- <%Object codeBOT = request.getParameter("codeBot");%> --%>
			<input type="hidden" name="codeBot" value="${param.codeBot}" />
			
			<table style="width: 100%;text-align: center;">
				<c:if test="${not empty sessionScope.beneSelected}">				
					<tr>
						<td class="titulosenblanco" colspan="3">
							Modificaci&oacute;n del Beneficio Otorgado
						</td>
					</tr>
					<tr>
						<td class="titulosenblanco" colspan="3">&nbsp;</td>
					</tr>
					<tr>
						<td class="titulosenblanco" colspan="3">
							<DIV align="center">Beneficiario</DIV>
						</td>
					</tr>
					<tr>
						<td class="titulosenblanco" colspan="3">&nbsp;</td>
					</tr>
					<logic:iterate id="bene" length="1" name="beneSelected">
						<tr>
							<td class="celdatabla" width="15%">&nbsp;</td>
							<td class="celdatabla" width="30%">Documento:</td>
							<td class="celdatabla" width="40%">
								<div class="form">
									<bean:write name="bene" property="numeroDocumento" />
								</div>
							</td>
						</tr>
						<tr>
							<td class="celdatabla" width="15%">&nbsp;</td>
							<td class="celdatabla" width="30%">Nombres:</td>
							<td class="celdatabla" width="40%">
								<div class="form">
									<bean:write name="bene" property="nombre1" />
									<bean:write name="bene" property="nombre2" />
								</div>
							</td>
							<td class="celdatabla" width="45%">&nbsp;</td>
						</tr>
						<tr>
							<td class="celdatabla" width="15%">&nbsp;</td>
							<td class="celdatabla" width="30%">Apellidos:</td>
							<td class="celdatabla" width="40%">
								<div class="form">
									<bean:write name="bene" property="primerApellido" />
									<bean:write name="bene" property="segundoApellido" />
								</div>
							</td>
							<td class="celdatabla" width="45%">&nbsp;</td>
						</tr>
					</logic:iterate>
					<tr>
						<td class="titulosenblanco" colspan="2">&nbsp;</td>
					</tr>
					<tr>
						<td class="titulosenblanco">&nbsp;</td>
						<td class="titulosenblanco" colspan="2"><hr /></td>
					</tr>				
				</c:if>
				<%  String stropcionfecha = "false";
    String valor = "";
    session.setAttribute("opcionfecha",stropcionfecha);
    String tamano = (String)session.getAttribute("tamanoLista");
    List lResultados = (List)session.getAttribute("listaresultados");
    List lValorFlex = (List)session.getAttribute("ListaAtributos"); 
    int tam=Integer.parseInt(tamano.trim());
    boolean bovalor = false;
    for(int i=0;i<tam;i++){
      bovalor = false; 
      AtributoVO atri = (AtributoVO)lValorFlex.get(i);
      ModificarBOTVO modifVO = new ModificarBOTVO();
      for(int k=0;k<lResultados.size();k++){
      	modifVO = (ModificarBOTVO)lResultados.get(k);
        if(modifVO.getNombre().equals(atri.getNombre())){
          bovalor = true;
          valor = modifVO.getValorInicial();
        }
      }
      if(valor == null) valor = "";%>
				<tr>
					<td class="celdatabla" width="15%">&nbsp;</td>
					<%
            String strobligatorio = atri.getObligatorio();
            String stretiqueta = "";
            if(strobligatorio.equals("S")){
              stretiqueta = atri.getEtiqueta()+"(*)";
            }else{
              stretiqueta = atri.getEtiqueta();
            }%>
					<td align="left" class="celdatabla"><%=stretiqueta%></td>
			<%
        if(atri.getValLista().equals("N")){
           if(atri.getTipodato().equals("F")){
              String nombre = atri.getCodatributo();
              stropcionfecha = "true";
              session.setAttribute("opcionfecha",stropcionfecha);
              
			%>
			
					<td align="left" class="celdatabla">
						<input id="Atri<%=nombre%>" name="Atri<%=nombre%>" type="text" class="calendario formInputOutput"
							value="<%=valor%>"/>						
						<input type="hidden" name="fecha" />					
		<%	}else{ 
              String strSize ="";
              if(Integer.parseInt(atri.getLongitud().trim())>40){
                strSize = "40";
              }else{
              	strSize = atri.getLongitud();
              }
              
              if(atri.getTipodato().equals("N")){
                 String strnom = atri.getCodatributo();
                 String strRuta = "validarnumero('Atri"+strnom+"')";
                 if((atri.getDescripcion()).equals("EDAD")){
                    String strNomAtri = (String)request.getAttribute("Edad");
                    if(strNomAtri != null && !strNomAtri.equals("")){%>
					<td align="left">
						<input class="formInputOutput" readonly="readonly"
							value="<%=strNomAtri%>" type="text" size="<%=strSize%>"
							onblur="validate(this)" maxlength="<%=atri.getLongitud()%>"
							name="Atri<%=atri.getCodatributo()%>" />
					</td>
					<%   
                    }else{
                      if(!bovalor){%>
					<td align="left"><input class="formInputOutput" type="text"
						size="<%=strSize%>" onblur="validate(this)"
						maxlength="<%=atri.getLongitud()%>"
						name="Atri<%=atri.getCodatributo()%>" /></td>
					<%
                      }else{%>
					<td align="left"><input class="formInputOutput" type="text"
						value="<%=valor%>" size="<%=strSize%>" onblur="validate(this)"
						maxlength="<%=atri.getLongitud()%>"
						name="Atri<%=atri.getCodatributo()%>" />
					</td>
					<% 
                      }
                    }
                  }else{
                    if(!bovalor){%>
					<td align="left"><input class="formInputOutput" type="text"
						size="<%=strSize%>" onblur="validate(this)"
						maxlength="<%=atri.getLongitud()%>"
						name="Atri<%=atri.getCodatributo()%>" /></td>
					<%
                    }else
                    {
%>
					<td align="left"><input class="formInputOutput" type="text"
						value="<%=valor%>" size="<%=strSize%>" onblur="validate(this)"
						maxlength="<%=atri.getLongitud()%>"
						name="Atri<%=atri.getCodatributo()%>" /></td>
					<%
                    }
                  }
              }else{
                 if(atri.getTipodato().equals("L")){
                   if(!bovalor){%>
					<td align="left"><input class="formInputOutput" type="text"
						size="<%=strSize%>" onblur="validateletras(this)"
						maxlength="<%=atri.getLongitud()%>"
						name="Atri<%=atri.getCodatributo()%>" /></td>
					<%
                   }else{
%>
					<td align="left"><input class="formInputOutput" value="<%=valor%>"
						type="text" size="<%=strSize%>" onblur="validateletras(this)"
						maxlength="<%=atri.getLongitud()%>"
						name="Atri<%=atri.getCodatributo()%>" /></td>
					<%
                   }
                  }else{
                     String value = "";
                     int sw = 0;
                     if(atri.getNombre().equals("NIVEL SISBEN")){
                        value = (String)request.getAttribute("nivel");
                        sw=1;
                     }
                     if(atri.getNombre().equals("PUNTAJE")){
                        value = (String)request.getAttribute("puntaje");
                        sw=1;
                     }
                     if(atri.getNombre().equals("CEDULA PADRE")){
                        value = (String)request.getAttribute("cedpadre");
                        sw=1;
                     }
                     if(atri.getNombre().equals("NOMBRE PADRE")){
                        value = (String)request.getAttribute("nompadre");
                        sw=1;
                     }
                     if(atri.getNombre().equals("TELEFONO PADRE")){
                        value = (String)request.getAttribute("telpadre");
                        sw=1;
                     }
                     if(atri.getNombre().equals("NOMBRE MADRE")){
                        value = (String)request.getAttribute("nommadre");
                        sw=1;
                     }
                     if(atri.getNombre().equals("CEDULA MADRE")){
                        value = (String)request.getAttribute("cedmadre");
                        sw=1;
                     }
                     if(atri.getNombre().equals("TELEFONO MADRE")){
                        value = (String)request.getAttribute("telmadre");
                        sw=1;
                     }
                     if(atri.getNombre().equals("NOMBRE ACUDIENTE")){
                        value = (String)request.getAttribute("nomacu");
                        sw=1;
                     }
                     if(atri.getNombre().equals("CEDULA ACUDIENTE")){
                        value = (String)request.getAttribute("cedacu");
                        sw=1;
                     }
                     if(atri.getNombre().equals("TELEFONO ACUDIENTE")){
                        value = (String)request.getAttribute("telacu");
                        sw=1;
                     }
                     if(atri.getNombre().equals("INSTITUCION EDUCATIVA")){
                        value = (String)request.getAttribute("nominsti");
                        sw=1;
                     }
                     if(atri.getNombre().equals("DIRECCION INST. EDUCATIVA")){
                        value = (String)request.getAttribute("dirinsti");
                        sw=1;
                     }
                     if(atri.getNombre().equals("TELEFONO INST. EDUCATIVA")){
                        value = (String)request.getAttribute("telinsti");
                        sw=1;
                     }
                     if(atri.getNombre().equals("NOMBRE SEDE")){
                        value = (String)request.getAttribute("nomsede");
                        sw=1;
                     }
                     if(atri.getNombre().equals("DIRECCION SEDE")){
                        value = (String)request.getAttribute("dirsede");
                        sw=1;
                     }
                     if(atri.getNombre().equals("TELEFONO SEDE")){
                        value = (String)request.getAttribute("telsede");
                        sw=1;
                     }
                     if(atri.getNombre().equals("GRADO")){
                        value = (String)request.getAttribute("grado");
                        sw=1;
                     }
                     if(atri.getNombre().equals("JORNADA")){
                        value = (String)request.getAttribute("jornada");
                        sw=1;
                     }
                     if(atri.getNombre().equals("NUCLEO EDUCATIVO")){
                        value = (String)request.getAttribute("nucleo");
                        sw=1;
                     }
                     
                     if(value == null) value = "";
                     if(sw==1){%>
					<td align="left"><input value="<%=value%>" class="formInputOutput"
						type="text" size="<%=strSize%>"
						maxlength="<%=atri.getLongitud()%>"
						name="Atri<%=atri.getCodatributo()%>" />
					</td>
					<%
                     }else{
                       if(!bovalor){
%>
					<td align="left"><input value="<%=value%>" class="formInputOutput"
						type="text" size="<%=strSize%>"
						maxlength="<%=atri.getLongitud()%>"
						name="Atri<%=atri.getCodatributo()%>" /></td>
					<%
                       }else{
%>
					<td align="left"><input value="<%=valor%>" class="formInputOutput"
						type="text" size="<%=strSize%>"
						maxlength="<%=atri.getLongitud()%>"
						name="Atri<%=atri.getCodatributo()%>" /></td>
					<%
                       }
                      }
                  }
              }
            }
%>
					<%
        }else{
          List listadominio = (List)atri.getValoresflexibles();
          int tamanodominio = listadominio.size();
          if(tamanodominio>0){
%>

					<td align="left">
						<select class="formInputOutput" name="Atri<%=atri.getCodatributo()%>">
							<%
          for(int j=0;j<tamanodominio;j++){          
            ValorFlexibleVO vflvo = (ValorFlexibleVO)listadominio.get(j);
            String ValorSugerido = atri.getDesvalorsugerido();
            String OpValorSugerido = vflvo.getCodigovfl();
            if((Object)ValorSugerido != null){            	
                if(vflvo.getCodigovfl().equals(valor) || vflvo.getDescripciontvfl().equals(valor)){
                	//que machetazo tan hp. Pero esto esta tan enredado que estoy que lo hago desde cero.                        
  %>
  							
  							<option selected="selected" value="<%=vflvo.getDescripcionvf()%>"><%=vflvo.getDescripcionvf()%></option>  														
							<%
                }else{
  %>
							<!-- <option value="<%--=vflvo.getCodigovfl()--%>"><%--=vflvo.getDescripcionvf()--%></option> -->
							<option value="<%=vflvo.getDescripcionvf()%>"><%=vflvo.getDescripcionvf()%></option>
							<%
                }
  %>
							<% 
            }else{
  %>
							<!-- <option Class="form" value="<%--=vflvo.getCodigovfl()--%>"><%--=vflvo.getDescripcionvf()--%></option> -->
							<option Class="form" value="<%=vflvo.getDescripcionvf()%>"><%=vflvo.getDescripcionvf()%></option>
							<%          }        
          }
%>
					</select></td>

					<%      }else{
%>
					<td align="left"><input class="formInputOutput" type="text"
						name="Atri<%=atri.getCodatributo()%>" /></td>
					<%
        }
      }
%>
				</tr>
				<%  }
%>
				<tr>
					<%
        String strCostBene = (String)request.getAttribute("CostBene");
      %>
					<td class="celdatabla" width="15%">&nbsp;</td>
					<td align="left" class="celdatabla">VALOR DEL BENEFICIO</td>
					<td align="left">
						<input type="TEXT" name="costbene" size="10"
							class="formInputOutput" value="<%=strCostBene%>" />
					</td>
				</tr>
				<tr>
					<%
        String strTipoBeneficio = (String)request.getAttribute("TipoBene");
	  %>
					<td class="celdatabla" width="15%">&nbsp;</td>
					<td align="left" class="celdatabla">TIPO DE BENEFICIO</td>
					<td align="left">
						<input type="TEXT" name="tipobene" size="75"
							class="formInputOutput" value="<%=strTipoBeneficio%>" />
					</td>
				</tr>
				
				<tr>
					<%
        				String codigoHechoVictimizante = (String)request.getAttribute("codigoHechoVictimizante");
	  				%>
					<td class="celdatabla" width="15%">&nbsp;</td>
					<td align="left" class="celdatabla">HECHO VICTIMIZANTE</td>
					<td align="left">
						<html:select property="hechoVictimizante" title="Sede"
							styleClass="formInputOutput" value="<%=(String) codigoHechoVictimizante%>">
							<html:option value="">Seleccione</html:option>
							<html:options name="hechosVictimizantes" labelName="codigoVflex" 
								labelProperty="descVflex" property="code" 
								collection="hechosVictimizantes" />
						</html:select>
					</td>
				</tr>
				<tr>
					<%
        String strDate = (String)request.getAttribute("DateBene");
      %>
					<td class="celdatabla" width="25%">&nbsp;</td>
					<td class="celdatabla" width="31%" align="left">
						FECHA DE OTORGACI&Oacute;N:
					</td>
					<td align="left">
						<input type="text" class="formInputOutput" name="date" id="date" value="<%=strDate%>" />
					</td>
				</tr>

				<tr>
					<td class="celdatabla" width="15%">&nbsp;</td>
					<td align="left" class="celdatabla">SEDE</td>
					<td align="left">
						<%
    					Object codeSede = session.getAttribute("codesede");
						%> 						
						<c:if test="${not empty sessionScope.ListaSede}">						
							<logic:iterate id="sede" name="ListaSede" length="1"
								type="co.gov.medellin.ssti.sub.model.vo.SedeVO">
								<html:select name="sede" property="code" title="Sede"
									styleClass="formInputOutput" value="<%=(String)codeSede%>">
									<html:options name="ListaSede" labelName="code"
										labelProperty="nombre" property="code" collection="ListaSede" />
								</html:select>
							</logic:iterate>											
						</c:if>
					</td>

				</tr>
				
				<tr>
					<td class="titulosenblanco" colspan="3">&nbsp;</td>
				</tr>
				<tr>
					<td align="right">&nbsp;</td>
					<td colspan="2">

						<DIV align="center">
							<input type="button" value="Guardar" name="guardarBtn" id="guardarBtn"/>
						</DIV>

					</td>

				</tr>

			</table>
			<div align="center">
				<a href="<%=request.getContextPath()+"/beneficios/VerBeneficios.do"%>">
					<img src="<%=request.getContextPath()+"/images/b_atras.gif"%>"
						width="24" height="15" border="0"><br> <span>Atras</span>
				</a>
			</div>		
		</c:if>
	</form>
</body>
</html>